package com.zwj.controller;

import com.zwj.pojo.AppCategory;
import com.zwj.pojo.AppInfo;
import com.zwj.pojo.DataDictionary;
import com.zwj.service.ByAppinfoService;
import com.zwj.service.ByCategoryService;
import com.zwj.service.ByDataDictionaryService;
import com.zwj.utils.Constants;
import com.zwj.utils.PageSupport;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/manager/backend/app")
public class AppChenkController {
    @Autowired
    private ByCategoryService byCategoryService;
    @Autowired
    private ByAppinfoService byAppinfoService;
    @Autowired
    private  ByDataDictionaryService dataDictionaryService;
    @RequestMapping("/list")
        public String getAppInfoList(Model model,
                @RequestParam(value="querySoftwareName",required=false) String querySoftwareName,
                @RequestParam(value="queryCategoryLevel1",required=false) String _queryCategoryLevel1,
                @RequestParam(value="queryCategoryLevel2",required=false) String _queryCategoryLevel2,
                @RequestParam(value="queryCategoryLevel3",required=false) String _queryCategoryLevel3,
                @RequestParam(value="queryFlatformId",required=false) String _queryFlatformId,
                @RequestParam(value="pageIndex",required=false) String pageIndex){
        List<AppInfo> appInfoList = null;
        List<DataDictionary> flatFormList = null;
        //列出一级分类列表，注：二级和三级分类列表通过异步ajax获取
        List<AppCategory> categoryLevel1List = null;
        List<AppCategory> categoryLevel2List = null;
        List<AppCategory> categoryLevel3List = null;

        //页面容量
        int pageSize = Constants.pageSize;
        //当前页码
        Integer currentPageNo = 1;
        if(pageIndex != null){
            currentPageNo = Integer.valueOf(pageIndex);
        }

        Integer queryCategoryLevel1 = null;
        if(_queryCategoryLevel1 != null && !_queryCategoryLevel1.equals("")){
            queryCategoryLevel1 = Integer.parseInt(_queryCategoryLevel1);
        }

        Integer queryCategoryLevel2 = null;
        if(_queryCategoryLevel2 != null && !_queryCategoryLevel2.equals("")){
            queryCategoryLevel2 = Integer.parseInt(_queryCategoryLevel2);
        }

        Integer queryCategoryLevel3 = null;
        if(_queryCategoryLevel3 != null && !_queryCategoryLevel3.equals("")){
            queryCategoryLevel3 = Integer.parseInt(_queryCategoryLevel3);
        }

        Integer queryFlatformId = null;
        if(_queryFlatformId != null && !_queryFlatformId.equals("")){
            queryFlatformId = Integer.parseInt(_queryFlatformId);
        }
        //总数量（表）
        int totalCount = byAppinfoService.getAppInfoCount(querySoftwareName,queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, queryFlatformId);
        //总页数
        PageSupport pages = new PageSupport();
        pages.setCurrentPageNo(currentPageNo);
        pages.setPageSize(pageSize);
        pages.setTotalCount(totalCount);
        int totalPageCount = pages.getTotalPageCount();
        //控制首页和尾页
        if(currentPageNo < 1){
            currentPageNo = 1;
        }else if(currentPageNo > totalPageCount){
            currentPageNo = totalPageCount;
        }

        appInfoList = byAppinfoService.getAppInfoList(querySoftwareName, queryCategoryLevel1, queryCategoryLevel2, queryCategoryLevel3, queryFlatformId, currentPageNo, pageSize);
        System.out.println("*******************");
        System.out.println(" = " + appInfoList);
        flatFormList = dataDictionaryService.getDataDictionaryList("APP_FLATFORM");
        categoryLevel1List = byCategoryService.getAppCategoryListByParentId(null);
        //把数据放到 model
        System.out.println("categoryLevel1List = " + categoryLevel1List);
        model.addAttribute("appInfoList", appInfoList);
        model.addAttribute("flatFormList", flatFormList);
        model.addAttribute("categoryLevel1List", categoryLevel1List);
        model.addAttribute("pages", pages);
        model.addAttribute("querySoftwareName", querySoftwareName);
        model.addAttribute("queryCategoryLevel1", queryCategoryLevel1);
        model.addAttribute("queryCategoryLevel2", queryCategoryLevel2);
        model.addAttribute("queryCategoryLevel3", queryCategoryLevel3);
        model.addAttribute("queryFlatformId", queryFlatformId);

        //二级分类列表和三级分类列表---回显
        if(queryCategoryLevel2 != null && !queryCategoryLevel2.equals("")){
            categoryLevel2List = getCategoryList(queryCategoryLevel1.toString());
            model.addAttribute("categoryLevel2List", categoryLevel2List);
        }
        if(queryCategoryLevel3 != null && !queryCategoryLevel3.equals("")){
            categoryLevel3List = getCategoryList(queryCategoryLevel2.toString());
            model.addAttribute("categoryLevel3List", categoryLevel3List);
        }
        return "backend/applist";
    }
    public List<AppCategory> getCategoryList (String pid){
        return byCategoryService.getAppCategoryListByParentId(pid==null?null:Integer.parseInt(pid));
    }
    @ResponseBody
    @RequestMapping(value="/categorylevellist.json",method= RequestMethod.GET)
    public List<AppCategory> getAppCategoryList (@RequestParam String pid){
        if(pid.equals("")) {
            pid = null;
        }
        List<AppCategory> appCategoryListByParentId = byCategoryService.getAppCategoryListByParentId(pid == null ? null : Integer.parseInt(pid));
        return appCategoryListByParentId;
    }
    @RequestMapping("/check")
    public  String SelectByget(int aid,int vid,Model model){
        AppInfo appBySelect = byAppinfoService.getAppBySelect(aid, vid);
        model.addAttribute("appInfo",appBySelect);
        return "backend/appcheck";
    }
    @PostMapping("checksave")
    public String checkSave(@RequestParam("id") Integer aid,@Param("status") Integer status){
        boolean b = byAppinfoService.updateSatus(status, aid);
        if(b=true){
            return "redirect:/manager/backend/app/list";
        }
        return "backend/appcheck";
    }
}
